package cn.iocoder.yudao.module.polling.service.pollingstandard;

import cn.iocoder.yudao.module.polling.convert.pollingcategoriesrelevance.PollingCategoriesRelevanceConvert;
import cn.iocoder.yudao.module.polling.convert.pollingpath.PollingPathConvert;
import cn.iocoder.yudao.module.polling.convert.pollingprojectsta.PollingProjectStaConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingcategoriesrelevance.PollingCategoriesRelevanceDO;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingpath.PollingPathDO;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingprojectsta.PollingProjectStaDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingcategoriesrelevance.PollingCategoriesRelevanceMapper;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingpath.PollingPathMapper;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingprojectsta.PollingProjectStaMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.polling.controller.admin.pollingstandard.vo.*;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingstandard.PollingStandardDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.polling.convert.pollingstandard.PollingStandardConvert;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingstandard.PollingStandardMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检标准 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingStandardServiceImpl implements PollingStandardService {

    @Resource
    private PollingStandardMapper standardMapper;

    @Resource
    private PollingCategoriesRelevanceMapper pollingCategoriesRelevanceMapper;

    @Resource
    private PollingPathMapper pollingPathMapper;

    @Resource
    private PollingProjectStaMapper pollingProjectStaMapper;

    @Override
    public Integer createStandard(PollingStandardCreateReqVO createReqVO) {
        // 插入
        PollingStandardDO standard = PollingStandardConvert.INSTANCE.convert(createReqVO);
        standardMapper.insert(standard);
        // 返回
        return standard.getId();
    }

    @Override
    public Integer createStandard(PollingStandardAddVo pollingStandardAddVo) {
        PollingStandardCreateReqVO createReqVO=new PollingStandardCreateReqVO();
        BeanUtils.copyProperties(pollingStandardAddVo,createReqVO);
        Integer id=createStandard(createReqVO);
       //todo 需要判断 是否添加过 pollingCategoriesRelevanceMapper.selectList()
        PollingCategoriesRelevanceDO pollingCategoriesRelevanceDO=new PollingCategoriesRelevanceDO();
        pollingCategoriesRelevanceDO.setPollingId(id);
        pollingCategoriesRelevanceDO.setCateId(Integer.parseInt(pollingStandardAddVo.getTypeId()));
        pollingCategoriesRelevanceMapper.insert(pollingCategoriesRelevanceDO);
       //插入设备 todo 需要判断 是否添加过
       List<PollingPathDO>  pollingPaths=Arrays.stream(pollingStandardAddVo.getImei()).map(a->{
            PollingPathDO pollingPath =new PollingPathDO();
            pollingPath.setBzId(id);
            pollingPath.setImeiCode(String.valueOf(a));
            return pollingPath;
        }).collect(Collectors.toList());
        pollingPathMapper.insertBatch(pollingPaths);
        //插入项目
        List<PollingProjectStaDO> list=Arrays.stream(pollingStandardAddVo.getItems()).map(a->{
            PollingProjectStaDO pollingProjectStaDO=new PollingProjectStaDO();
            pollingProjectStaDO.setBzId(id);
            pollingProjectStaDO.setItemId(a);
            return pollingProjectStaDO;
        }).collect(Collectors.toList());
        pollingProjectStaMapper.insertBatch(list);
        return id;
    }

    @Override
    public void updateStandard(PollingStandardUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStandardExists(updateReqVO.getId());
        // 更新
        PollingStandardDO updateObj = PollingStandardConvert.INSTANCE.convert(updateReqVO);
        standardMapper.updateById(updateObj);
    }

    @Override
    public void deleteStandard(Integer id) {
        // 校验存在
        this.validateStandardExists(id);
        // 删除
        standardMapper.deleteById(id);
    }

    private void validateStandardExists(Integer id) {
        if (standardMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingStandardDO getStandard(Integer id) {
        return standardMapper.selectById(id);
    }

    @Override
    public List<PollingStandardDO> getStandardList(Collection<Integer> ids) {
        return standardMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingStandardDO> getStandardPage(PollingStandardPageReqVO pageReqVO) {
        return standardMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingStandardDO> getStandardList(PollingStandardExportReqVO exportReqVO) {
        return standardMapper.selectList(exportReqVO);
    }

    @Override
    public List<PollingStandardQueryVo> selectPollingStandardMList(PollingStandardQueryVo pollingStandardQueryVo) {
        return standardMapper.selectPollingStandardMList(pollingStandardQueryVo);
    }

}
